import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';
const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表'
});
const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});
const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增'
});
const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改'
});
const confirmDesc = formatMessage({
    id: 'api.confirm',
    defaultMessage: '确认'
});
const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});
const getCancelDesc = formatMessage({
    id: 'api.getCancel',
    defaultMessage: '取消Sub PI'
});
const getBankDesc = formatMessage({
    id: 'api.message.getBankDesc',
    defaultMessage: '查询开户银行列表'
});
const getSubDetailDesc = formatMessage({
    id: 'api.message.getSubDetailDesc',
    defaultMessage: '获取Sub PI最近主单'
});
const getCarrierDesc = formatMessage({
    id: 'api.message.getCarrierDesc',
    defaultMessage: '查询货代信息'
});
const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});
const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '导入模板'
});
const getExcelDesc = formatMessage({
    id: 'api.message.getExcelDesc',
    defaultMessage: '下载Excel'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/subPiKd/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/subPiKds/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/subPiKds/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/subPiKds`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/subPiKds/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 确认
export const confirmData = (id, data) => request(`${baseUrl}/subPiKds/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/subPiKds/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// 取消pi
export const cancelData = (id, rowVersion) => request(`${baseUrl}/subPiKds/${id}/cancel`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getCancelDesc));

// 查询货代信息
export const getCarriers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/carriers/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getCarrierDesc));
};

// 查询开户银行列表
export const getBanks = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/banks/forSubPiKd?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getBankDesc));
};

//获取Sub PI最近主单
export const getSubDetail = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/subPiKds/details?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSubDetailDesc));
};

// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

//导入备件清单模板
export const partImportTemp = () => request(`${baseUrl}/subPiKds/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);

//导出Excel
export const getPiExcel = id => request(`${baseUrl}/subPiKds/${id}/exportExcel`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getExcelDesc))
    .then(res => res.data);
